﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DB = HTComics.Data;

namespace HTComics.Web.User
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["logout"] != null && Request.QueryString["logout"] == "1")
            {
                Session.Abandon();
            }
            else if (Request.QueryString["ord"] != null && Request.QueryString["ord"] == "1") //For Submit Ord
            {
                lblError.Text = "Please login to the site to submit your order";
                lblError.Visible = true;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            DataTable dt = DB.HTCUser.Login(txtLogin.Text, txtPassword.Text);

            if (dt.Rows.Count == 0)
            {
                lblError.Text = "Incorrect User / Password, please try again";
                lblError.Visible = true;
            }
            else
            {
                //Initiate User Session
                Session["UID"] = dt.Rows[0]["UserId"].ToString();
                Session["FirstName"] = dt.Rows[0]["FirstName"].ToString();
                Session["LastName"] = dt.Rows[0]["LastName"].ToString();
                Session["Email"] = dt.Rows[0]["Email"].ToString();

                if (Request.QueryString["ord"] != null && Request.QueryString["ord"] == "1") //For Submit Ord
                    Response.Redirect("~/Order/SubmitOrder.aspx");
                else
                    Response.Redirect("~/Default.aspx");
            }
            
        }

        protected void btnForgotPass_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["ord"] != null && Request.QueryString["ord"] == "1") //For Submit Ord
                Response.Redirect("~/User/ForgotPassword.aspx?ord=1");
            else
                Response.Redirect("~/User/ForgotPassword.aspx");
        }
    }
}